package com.example.news.helper;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class gethttp extends Thread {
    public String m_url;
    public Handler handler;
    public gethttp(String url, Handler handler) {
        this.m_url=url;
        this.handler=handler;
    }
    @Override
    public void run() {
        HttpURLConnection connection=null;
        try {
            URL url=new URL(m_url);
            connection=(HttpURLConnection) url.openConnection();
            connection.connect();
//            BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
            DataInputStream ios=new DataInputStream(new DataInputStream(connection.getInputStream()));

            InputStream in=connection.getInputStream();
            BufferedReader reader=null;
            reader=new BufferedReader(new InputStreamReader(in));
            StringBuilder response=new StringBuilder();
            String line;
            while ((line=reader.readLine())!=null){

                response.append(line);
            }
            Message msg=new Message();
            msg.what=1;
            msg.obj=response.toString();
            handler.sendMessage(msg);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(connection!=null)
            {
                connection.disconnect();
            }
        }
    }
}

